%Scatter plots with R^2 values for the
function fig_CIP_ccbVSfreeBetabetasScatter(CIPCalcsAll,coeffs,currSelector,optfig)

%First select out CIP data for recent period excluding holidays
CIPCalcsAll2 = CIPCalcsAll(3077:end,:);
selectorCIP = zeros(315,1)+1;
selectorCIP([8 9 13 65 85 130 195 260 270 275],:) = 0;
selectorCIP = (selectorCIP>0);
CIPCalcsAll2 = CIPCalcsAll2(selectorCIP,1:5)*1e4;

%For graphs of extremes of price impact
truncationpoint = 0.10;
truncationselector = coeffs(:,currSelector)>truncationpoint;
R2s = [0 0];
R2s(1,1) = corr(coeffs(:,currSelector),CIPCalcsAll2(:,currSelector))^2;
R2s(1,2) = corr(coeffs(truncationselector,currSelector),CIPCalcsAll2(truncationselector,currSelector))^2;
[X,Y,BetaHat,YHat,ehat,Sandwich]=OLSGenericProcessor(coeffs(truncationselector,currSelector),CIPCalcsAll2(truncationselector,currSelector));
CurrencyNames = {' AUD/USD',' GBP/USD',' CAD/USD',' EUR/USD',' JPY/USD'};
CurrencyNamesFileNames = {'AUDUSD','GBPUSD','CADUSD','EURUSD','JPYUSD'};

functionFolder = cd('.');

if optfig.plotfig ==1
    
    [fontname,dimension,lw,folder,color,style,marker,markersize,fontsize_tit,fontsize_ax,fontsize_lab,fontsize_leg] = fn_optfig(optfig);
    
    fig = figure('Units','inches','Position',dimension);
    
    scatter(coeffs(:,currSelector),CIPCalcsAll2(:,currSelector))
    ylabel('CIP Deviations (basis points)','interpreter','latex','FontSize',fontsize_lab,'FontName',fontname)
    xlabel('Coefficient $\beta$','interpreter','latex','FontSize',fontsize_lab,'FontName',fontname)
    title(strcat(CurrencyNames{1,currSelector}), 'interpreter','latex','FontSize',fontsize_tit,'FontName',fontname)
    grid; box off;
    annotation(fig,'textbox',...
        [0.668056019070322 0.186195826645265 0.127724672228844 0.0449438202247191],...
        'String',{strcat('$$R^2$$','=',num2str(round(R2s(1,2),3)))},...
        'FitBoxToText','off','interpreter','latex','FontSize',fontsize_leg,'FontName',fontname,'LineStyle','none');
end

%Now select out only extreme events

oldFolder = cd('output\'); %#ok<NASGU>
save(strcat('Correlations_ScatterFreeBetaBeta',CurrencyNamesFileNames{1,currSelector}),'R2s');
saveas(gcf,'xx_ScatterCCBvsFreeBetaBetas','epsc')
saveas(gcf,strcat('xx_ScatterCCBvsFreeBetaBetas',CurrencyNamesFileNames{1,currSelector}),'epsc')
cd(functionFolder);


end